package com.etc.controller;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.Orders;
import com.etc.service.OrderService;
import com.etc.util.Message;
import com.etc.util.PageData;

@Controller
@RequestMapping(value = "orderapi")
public class OrderController {

	@Resource
	private OrderService os;

	@Resource(name = "message")
	private Message msg;

	private Logger log = Logger.getLogger(UserController.class);

	@RequestMapping(value = "pagedata", method = RequestMethod.GET)
	// 响应json
	@ResponseBody
	public PageData<Orders> selectAllByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit,
			@RequestParam(value = "content", defaultValue = "") String content) {
		PageData<Orders> pd = os.selectAllByPage(page, limit, content);
		pd.setCode(0);
		pd.setMsg("获取到数据咯！");
		return pd;
	}

	/**
	 * 删除的方法
	 * 
	 * @param orderid
	 * @return
	 */
	@RequestMapping(value = "order/{orderId}", method = RequestMethod.DELETE)
	@ResponseBody
	public Message deleteByPrimaryKey(@PathVariable(value = "orderId") int orderid) {
		boolean flag = os.deleteByPrimaryKey(orderid);
		if (flag) {
			msg.setMsg("删除成功");
		} else {
			msg.setMsg("删除失败");
		}
		return msg;
	}

	/**
	 * 添加的方法
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "order", method = RequestMethod.POST)
	@ResponseBody
	public Message insertSelective(@RequestBody Orders order) {
		boolean flag = os.insertSelective(order);
		if (flag) {
			msg.setMsg("添加成功");
		} else {
			msg.setMsg("添加失败");
		}
		return msg;
	}

	/**
	 * 查询单个订单的方法
	 *
	 * @param orderid
	 * @return
	 */
	@RequestMapping(value = "order/{orderId}", method = RequestMethod.GET)
	@ResponseBody
	public Orders selectByPrimaryKey(@PathVariable(value = "orderId") int orderid) {
		Orders order = os.selectByPrimaryKey(orderid);
		return order;
	}

	/**
	 * 修改的方法
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "order", method = RequestMethod.PUT)
	@ResponseBody
	public Message updateByPrimaryKeySelective(@RequestBody Orders order) {
		boolean flag = os.insertSelective(order);
		if (flag) {
			msg.setMsg("修改成功");
		} else {
			msg.setMsg("修改失败");
		}
		return msg;
	}

	@RequestMapping(value = "pagedata/{userId}", method = RequestMethod.GET)
	// 响应json
	@ResponseBody
	public PageData<Orders> selectByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit,
			@RequestParam(value = "content", defaultValue = "") String content,
			@PathVariable(value = "userId") int userid) {
		PageData<Orders> pd = os.selectByPage(page, limit, content, userid);
		pd.setCode(0);
		pd.setMsg("获取到数据咯！");
		return pd;
	}
}
